package com.yijiaganxi.product.web.product;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yijiaganxi.common.entity.product.YjgxProduct;
import org.yijiaganxi.common.res.R;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.yijiaganxi.common.GlobalConstants;
import com.yijiaganxi.product.service.product.YjgxProductService;

import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 商品管理 前端控制器
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Validated
@RestController
@RequestMapping("/product/yjgxProduct/" +GlobalConstants.WEB_FLAG)
public class YjgxProductController {

	@Autowired
	public YjgxProductService productService;
	
	/**
	 * 查询商品发分页数据
	 * 
	 * @param cureentPage
	 * @param size
	 * @return
	 */
	@GetMapping("selectProductPageList")
	public R selectProductPageList(@NotBlank(message = "cureentPage不能为空") Integer cureentPage,
			@NotBlank(message = "size不能为空") Integer size) {
		Page<YjgxProduct> selectPage = productService.selectPage(new Page<YjgxProduct>(cureentPage, size),
				new EntityWrapper<YjgxProduct>());
		return R.ok().put("data", selectPage);
	}

	/**
	 * 根据条件查询商品分页数据
	 * 
	 * @param name
	 * @param catetoryId
	 * @param cureentPage
	 * @param size
	 * @return
	 */
	@GetMapping("selectProductPageListByCondition")
	public R selectProductPageListByCondition(String name, String categoryId,
			@NotBlank(message = "cureentPage不能为空") Integer cureentPage, @NotBlank(message = "size不能为空") Integer size) {
		EntityWrapper<YjgxProduct> entityWrapper = new EntityWrapper<YjgxProduct>();

		if (StringUtils.isNotBlank(name))
			entityWrapper.like("name", name);

		if (StringUtils.isNotBlank(categoryId)) {
			entityWrapper.and().eq("categoryId", categoryId);
		}

		Page<YjgxProduct> selectPage = productService.selectPage(new Page<YjgxProduct>(cureentPage, size),
				entityWrapper);
		return R.ok().put("data", selectPage);
	}

	/**
	 * 添加商品
	 * 
	 * @param product
	 * @return
	 */
	@PostMapping("addProduct")
	public R addProduct(@Validated YjgxProduct product) {
		return R.insertOrUpdateResponse(productService.insert(product));
	}

	/**
	 * 更新商品
	 * 
	 * @param product
	 * @return
	 */
	@PostMapping("updateProduct")
	public R updateProduct(YjgxProduct product) {
		if (product.getId() == null)
			return R.error("商品ID必填");
		return R.insertOrUpdateResponse(productService.updateById(product));
	}
	
	
	/**
	 * 删除商品
	 * @param id
	 * @return
	 */
	@PostMapping("daleteProductById")
	public R daleteProductById( @NotBlank(message = "商品ID不能为空") String id) {
		return R.insertOrUpdateResponse(productService.deleteById(id));
	}
}
